package com.yaoxun.bored.controller.sys;

import java.util.Date;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.yaoxun.bored.common.BaseController;
import com.yaoxun.bored.common.Constant;
import com.yaoxun.bored.common.JsonObject;
import com.yaoxun.bored.common.PageResult;
import com.yaoxun.bored.model.sys.auth.Auth;
import com.yaoxun.bored.model.sys.auth.AuthQuery;
import com.yaoxun.bored.service.sys.auth.AuthService;

@RequestMapping("/auth")
@Controller
public class AuthController extends BaseController {

	private static final String INDEX_PAGE = "sys/auth/auth";
	
	private static final String EDIT_PAGE = "sys/auth/auth_edit";
	
	@Inject
	private AuthService authService;
	
	@RequestMapping(value="/save", method=RequestMethod.GET)
	public ModelAndView save(@RequestParam Integer menuId) {
		Auth auth = new Auth();
		auth.setMenuId(menuId);
		ModelAndView mv = new ModelAndView(EDIT_PAGE);
		mv.addObject(Constant.MV_OBJ_NAME, auth);
		return mv;
	}
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject save(Auth auth) {
		JsonObject jsonObject = new JsonObject();
		auth.setCreateTime(new Date());
		authService.insert(auth);
		return jsonObject;
	}
	
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public ModelAndView edit(@RequestParam Integer id) {
		ModelAndView mv = new ModelAndView(EDIT_PAGE);
		Auth auth = authService.selectByPrimaryKey(id);
		mv.addObject(Constant.MV_OBJ_NAME, auth);
		return mv;
	}
	
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject edit(Auth auth) {
		JsonObject jsonObject = new JsonObject();
		auth.setModifyTime(new Date());
		authService.updateByPrimaryKeySelective(auth);
		return jsonObject;
	}
	
	@RequestMapping(value="/del", method=RequestMethod.POST)
	@ResponseBody
	public JsonObject delete(@RequestParam("authIds[]") Integer[] authIds) {
		JsonObject jsonObject = new JsonObject();
		
		return jsonObject;
	}
	
	@RequestMapping(value="/search")
	@ResponseBody
	public PageResult search(AuthQuery query) {
		PageInfo<Map<String,Object>> info = authService.selectByPaging(query);
		PageResult pageResult = new PageResult(info.getTotal(), info.getList());
		return pageResult;
	}
	
	@RequestMapping(value="/index", method=RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView(INDEX_PAGE);
		return mv;
	}
	
}
